import { _decorator, Component, EventHandler, Label, Node, Sprite } from 'cc';
import { LevelUpCom } from '../../Entity/Component/LevelUpCom';
import { Global, SkillData, SkillType, UnitType } from '../../Util/Global';
import { UICtrl } from '../../Ctrl/UICtrl';
import { GameSticker, GameStickerType } from '../../Model/GameStickerMgr';
import { AimTipSticker } from '../../Entity/UI/AimTipSticker';
import { Unit } from '../../Entity/Unit';
import { FactoryCom } from '../../Entity/Component/FactoryCom';
import { SpawnEvent } from '../../Entity/Component/SpawnerCom';
import { BulletLaser } from '../../Entity/BulletLaser';
import GameEventMgr, { GameEvent } from '../../Model/GameEventMgr';
const { ccclass, property } = _decorator;

@ccclass('SkillButtonItem')
export class SkillButtonItem extends Component {

    static onClickHoldItem: SkillButtonItem = null;

    @property({ type: Sprite, displayName: "icon" })
    public icon: Sprite = null;
    @property({ type: Label, displayName: "预备数量" })
    public count: Label = null;
    @property({ type: Label, displayName: "等级" })
    public level: Label = null;
    @property({ type: Sprite, displayName: "生产进程遮罩" })
    public progress_mask: Sprite = null;

    public levelCom: LevelUpCom = null;
    public skillType: SkillType = null;

    protected start(): void {
        GameEventMgr.Register(this, GameEvent.UnitEnterScreenField, this.refreshSkillCouldSelect.bind(this))
    }

    protected refreshSkillCouldSelect(unit: Unit) {
        if (SkillButtonItem.onClickHoldItem != this
            || unit.unitType <= UnitType.UnitSpacecra || unit.unitType >= UnitType.UnitGalaxy) {
            return;
        }
        let player = Global.gameManager.playerUnit;
        if (player != unit) {
            if (this.skillType == SkillType.LightParticleStrike) {
                // UICtrl.ClearStickerRenderer(unit, GameStickerType.AimTipSticker);
                let sticker_node = UICtrl.CreateStickerRenderer(unit, GameStickerType.AimTipSticker);
                this.onClickStickerCallback_LightParticleStrike(sticker_node);
            }
        }
    }

    protected update(dt: number): void {
        if (this.levelCom && this.levelCom.isValid) {
            let skill = this.levelCom.skillMap.get(this.skillType);
            if (skill) {
                this.level.string = 'lv' + (skill.lv || '');
                let areadyTime = skill.readyTime;
                let coolTime = SkillData.SkillMap.get(this.skillType).coolTime;

                let readyCount = Math.floor(areadyTime / coolTime);
                if (readyCount < skill.maxCount) {
                    let notReadyTime = areadyTime - readyCount * coolTime;
                    this.progress_mask.fillRange = notReadyTime / coolTime;
                }
                else {
                    this.progress_mask.fillRange = 0;
                }

                let couldCount = Math.min(skill.maxCount, readyCount);
                this.count.string = (couldCount || '') + '';
            }

        }
    }

    public OnClick(event: EventHandler, customEventData: string) {
        // console.log('技能');
        if (SkillButtonItem.onClickHoldItem) {
            // this.skill_releace_cancel();
            SkillButtonItem.onClickHoldItem.skill_releace_cancel();
            SkillButtonItem.onClickHoldItem = null;
            return;
        }

        let skillData = SkillData.SkillMap.get(this.skillType);
        let skill = this.levelCom.skillMap.get(this.skillType);
        if (skill.readyTime < skillData.coolTime) {
            return;//冷却
        }

        SkillButtonItem.onClickHoldItem = this;

        if (this.skillType == SkillType.LightParticleStrike) {

            let filter = (u: Unit) => {
                if (u.isInScreenPos) {
                    this.refreshSkillCouldSelect(u);
                }
            }

            // Global.gameManager.currentUnitLiveMap.get(UnitType.UnitSpacecra).forEach(filter);
            Global.gameManager.currentUnitLiveMap.get(UnitType.UnitCommonStar).forEach(filter)
        }
    }

    private onClickStickerCallback_LightParticleStrike(sticker_node: Node) {
        if (sticker_node) {
            let aim_sticker = sticker_node.getComponent(AimTipSticker);
            if (aim_sticker) {
                if (aim_sticker.target.playerCtrl) {
                    UICtrl.ClearStickerRenderer(aim_sticker.target, GameStickerType.AimTipSticker);
                    return;
                }
                aim_sticker.onClickCallback = this.skill_release_confirm.bind(this);
            }
        }
    }

    /**释放确认 */
    private skill_release_confirm(target: Unit) {
        SkillButtonItem.onClickHoldItem = null;
        let skillData = SkillData.SkillMap.get(this.skillType);
        let skill = this.levelCom.skillMap.get(this.skillType);
        let can = false;
        if (skill.readyTime >= skillData.coolTime) {

            skill.readyTime -= skillData.coolTime;
            can = true;
        }

        if (can) {
            if (this.skillType == SkillType.LightParticleStrike) {
                UICtrl.ClearStickerRenderer(null, GameStickerType.AimTipSticker);

                let player = Global.gameManager.playerUnit;
                let fa = player.getComponent(FactoryCom);
                fa.spawner.onSpawnEventCallback('playerSkillBullet', {
                    SpawnEventCallback(unit, event) {
                        if (event == SpawnEvent.SPAWNING && unit.unitType == UnitType.Bullet) {

                            let bulletLaser = unit.getComponent(BulletLaser);
                            if (bulletLaser) {
                                bulletLaser.SetTarget(target);
                                bulletLaser.lv = skill.lv;
                                fa.spawner.offSpawnEventCallback('playerSkillBullet', this);
                                return false;//取用
                            }
                        }
                        return true;
                    },
                });
                fa.spawner.AddProductNum('BulletLaser', 1);
            }
        }
    }

    /**释放取消 */
    private skill_releace_cancel() {
        SkillButtonItem.onClickHoldItem = null;
        if (this.skillType == SkillType.LightParticleStrike) {
            UICtrl.ClearStickerRenderer(null, GameStickerType.AimTipSticker);
        }
    }
}

